using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 列出上游DNS服务器命令类
    /// 作者: huobing
    /// </summary>
    public sealed class ListUpstreamCommand : BaseCommand<ListUpstreamResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public const string NAME = "LIST_UPSTREAM";
        
        /// <summary>
        /// 获取命令名称
        /// </summary>
        public override string Name => NAME;
        
        /// <summary>
        /// 获取命令格式
        /// </summary>
        public override string Format => "LIST_UPSTREAM";
        
        /// <summary>
        /// 获取命令描述
        /// </summary>
        public override string Description => "列出所有上游DNS服务器";
        
        /// <summary>
        /// 获取命令权限级别
        /// </summary>
        public override CommandPermission Permission => CommandPermission.User;
        
        /// <summary>
        /// 验证命令参数
        /// </summary>
        /// <param name="args">命令参数数组</param>
        /// <param name="error">错误信息输出</param>
        /// <returns>验证是否通过</returns>
        public override bool Validate(string[] args, out string error) 
        {
            error = string.Empty;
            return args.Length == 0 || (error = "此命令不接受参数") == string.Empty;
        }
    }
    
    /// <summary>
    /// 列出上游DNS服务器命令的返回实体类
    /// 作者: huobing
    /// </summary>
    public class ListUpstreamResult : BaseCommandResult
    {  
        /// <summary>
        /// 上游服务器列表
        /// </summary>
        public List<UpstreamServerInfo> UpstreamServers { get; set; } = new List<UpstreamServerInfo>();
        
        /// <summary>
        /// 控制台输出格式化
        /// </summary>
        /// <returns>格式化后的上游服务器列表信息</returns>
        public override string ToConsoleString()
        {  
            var sb = new System.Text.StringBuilder();
            
            if (!string.IsNullOrEmpty(Message))
            {
                sb.AppendLine(Message);
            }
            else
            {
                sb.AppendLine("上游DNS服务器列表：");
            }
            
            if (UpstreamServers == null || UpstreamServers.Count == 0)
            {
                sb.AppendLine("暂无上游服务器配置");
            }
            else
            {
                // 准备表格数据
                var tableData = new List<string[]>();
                tableData.Add(new[] { "地址", "缓存数", "主域名", "是否默认" }); // 表头
                
                foreach (var server in UpstreamServers)
                {
                    tableData.Add(new[]
                    {
                        server.Address,
                        server.CacheCount.ToString(),
                        server.MainDomain ?? "-",
                        server.IsDefault ? "是" : "否"
                    });
                }
                
                // 使用TextTableFormatter格式化表格
                string table = LDNS.SDK.Formatting.TextTableFormatter.FormatAlignedTable(tableData, ' ', 80);
                sb.AppendLine(table);
            }
            
            return sb.ToString().TrimEnd();
        }
    }
 
    /// <summary>
    /// 上游DNS服务器信息类
    /// 存储上游DNS服务器的详细信息
    /// 作者：huobing
    /// </summary>
    public class UpstreamServerInfo
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string Address { get; set; } = string.Empty;
        
        /// <summary>
        /// 缓存数量
        /// </summary>
        public int CacheCount { get; set; }
        
        /// <summary>
        /// 主域名
        /// </summary>
        public string? MainDomain { get; set; }
        
        /// <summary>
        /// 是否为默认服务器
        /// </summary>
        public bool IsDefault { get; set; }
    }   

}